package com.xbj.cloud.commons.resp;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;

public enum ReturnCodeEnum {
    //举值 - 构造 - 遍历
    //举值
    RC999("999","操作XXX失败"),
    RC200("200","success"),
    RC403("403","无权限访问,请联系管理员授予权限"),
    RC404("404","页面丢失"),
    RC500("500","系统异常,请稍后重试");

    //构造
    private final String code;
    private final String message;

    public String getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    ReturnCodeEnum(String code, String message) {
        this.code = code;
        this.message = message;
    }
    //遍历1
    public static ReturnCodeEnum getReturnCodeEnumV1(String code){
        for (ReturnCodeEnum returnCodeEnum : ReturnCodeEnum.values()) {
            if (StringUtils.equals(returnCodeEnum.getCode(),code)){
                return returnCodeEnum;
            }
        }
        return null;
    }

    //遍历2
    public  static ReturnCodeEnum getReturnCodeEnumV2(String code){
        return Arrays.stream(ReturnCodeEnum.values()).filter(x -> StringUtils.equals(x.getCode(), code)).findFirst().orElse(null);
    }

    public static void main(String[] args) {
        System.out.println(ReturnCodeEnum.getReturnCodeEnumV1("200"));
        System.out.println(ReturnCodeEnum.getReturnCodeEnumV1("200").getCode());
        System.out.println(ReturnCodeEnum.getReturnCodeEnumV1("200").getMessage());

        System.out.println(ReturnCodeEnum.getReturnCodeEnumV2("404"));
        System.out.println(ReturnCodeEnum.getReturnCodeEnumV2("404").getCode());
        System.out.println(ReturnCodeEnum.getReturnCodeEnumV2("404").getMessage());
    }
}
